package com.yphone.ui.ysj.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.iiordanov.bVNC.App;
import com.iiordanov.bVNC.ConnectionBean;
import com.yphone.ui.event.MyEvent;
import com.yphone.ui.pay.activity.PayActivity;
import com.zzkj.yunphone.R;
import com.iiordanov.bVNC.RemoteCanvasActivity;
import com.iiordanov.bVNC.Utils;
import com.iiordanov.bVNC.event.bVNCEvent;
import com.yphone.base.utils.SharepreferenceUtils;
import com.yphone.tools.GlideManager;
import com.yphone.tools.MyLog;
import com.yphone.tools.RemoteUtils;
import com.yphone.tools.SharepreferenConstants;
import com.yphone.tools.glide.GlideRoundTransform;
import com.yphone.ui.dialog.AlterDisDialog;
import com.yphone.ui.dialog.inf.IAlterDisCallBack;
import com.yphone.ui.login.manager.LoginManager;
import com.yphone.ui.login.respone.UserInfo;
import com.yphone.ui.ysj.bean.RemoteConnectBean;
import com.yphone.utils.AccountUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class YSJNewWelFarePageView extends LinearLayout implements OnClickListener {
    private ImageView imgNewWelFare;
    private Context mContext;
    private RemoteConnectBean bean;
    private TextView index_phone_title;
    private TextView index_time;

    public YSJNewWelFarePageView(Context context, RemoteConnectBean bean) {
        super(context);
        this.bean = bean;
        this.mContext = context;
        initView();
        initData();
        initListen();
    }

    public YSJNewWelFarePageView(Context context) {
        this(context, (AttributeSet) null);
    }

    public YSJNewWelFarePageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public YSJNewWelFarePageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
        initData();
        initListen();
    }

    private void initData() {
        index_phone_title.setText(bean.getName());
        index_time.setText(mContext.getString(R.string.residue_time_str, AccountUtils.getDayAndHourTime(bean.timeHour)));
    }

    private void initView() {
        EventBus.getDefault().register(this);
        LayoutInflater.from(getContext()).inflate(R.layout.view_ysj_new_user_view, this);
        index_phone_title = findViewById(R.id.index_phone_title);
        index_time = findViewById(R.id.index_time);

        this.imgNewWelFare = (ImageView) findViewById(R.id.img_newwelfare);

        Bitmap bitmap = null;
        try {
            byte[] bitmapByte = Base64.decode(bean.view, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapByte, 0, bitmapByte.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        imgNewWelFare.setImageBitmap(bitmap);

//        if (!TextUtils.isEmpty(bean.getImgPath())) {
//            if (bean.getImgPath().startsWith("http")) {
//                GlideManager.glide(this.imgNewWelFare.getContext(), this.imgNewWelFare, bean.getImgPath(), new CenterCrop(getContext()), new GlideRoundTransform(this.imgNewWelFare.getContext(), 26));
//            } else {
//                GlideManager.glideFile(this.imgNewWelFare.getContext(), this.imgNewWelFare, new File(bean.getImgPath()), new CenterCrop(getContext()), new GlideRoundTransform(this.imgNewWelFare.getContext(), 26));
//            }
//        }

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        EventBus.getDefault().unregister(this);
    }

    private void initListen() {
        this.imgNewWelFare.setOnClickListener(this);
//        findViewById(R.id.index_edit_img).setOnClickListener(this);
        findViewById(R.id.view_ysj_index_recharge).setOnClickListener(this);
        findViewById(R.id.ff_index_edit).setOnClickListener(this);
    }

    public void onClick(View v) {
        if (v.getId() == this.imgNewWelFare.getId()) {
            List<RemoteConnectBean> remoteConnectBeans = LoginManager.getInstance().getInfo().remoteConnectBeans;
            for (int i = 0; i < remoteConnectBeans.size(); i++) {
                RemoteConnectBean remoteConnectBean = remoteConnectBeans.get(i);
                if (remoteConnectBean.equals(bean)) {
                    RemoteUtils.toRemote(getContext(), bean);
                }
            }
        } else if (v.getId() == R.id.index_edit_img||v.getId() == R.id.ff_index_edit) {
            showAlterDialog();
        } else if (v.getId() == R.id.view_ysj_index_recharge) {
            PayActivity.toPayActivity(getContext(), bean);
        }

    }

    private void showAlterDialog() {
        Context context = getContext();
        Log.e("XA", "context name = " + context.getClass().getName());
        EventBus.getDefault().post(new MyEvent.ShowDeviceNameEdit(bean));
//        AlterDisDialog.showDialog(getContext(), "", false, new IAlterDisCallBack() {
//            public void callback(String dis) {
//                index_phone_title.setText(dis);
//                UserInfo userInfo = LoginManager.getInstance().getInfo();
//                ArrayList<RemoteConnectBean> remoteConnectBeans = userInfo.remoteConnectBeans;
//                for (int i = 0; i < remoteConnectBeans.size(); i++) {
//                    RemoteConnectBean remoteConnectBean = remoteConnectBeans.get(i);
//                    if (remoteConnectBean.equals(bean)) {
//                        remoteConnectBean.nickname = dis;
//                        SharepreferenceUtils.saveClass(App.getContext(), SharepreferenConstants.SHARE_NAME_FILE, SharepreferenConstants.USER_INFO_NODE, userInfo);
//                    }
//                }
////                YSJCloudHomePageView.this.mDis = dis;
////                YSJCloudHomePageView.this.ysjCloundHomePagePresenter.sendAlterRequest(YSJCloudHomePageView.this.info.OrderId, dis);
//            }
//        });
    }


    @Subscribe
    public void onEventMainThread(bVNCEvent.UInotifyEvent event) {
        MyLog.e("eventbus msg handle "+bean.getImgPath());
        if (event.isLogout){
            return;
        }
        UserInfo info = LoginManager.getInstance().getInfo();
        List<RemoteConnectBean> remoteConnectBeans = info.remoteConnectBeans;

        for (int i = 0;i<remoteConnectBeans.size();i++){
            RemoteConnectBean remoteConnectBean = remoteConnectBeans.get(i);
            if (remoteConnectBean.equals(bean)){
                MyLog.e("id = "+remoteConnectBean.id+"  view is null = "+TextUtils.isEmpty(remoteConnectBean.view));
                Bitmap bitmap = null;
                try {
                    byte[] bitmapByte = Base64.decode(remoteConnectBean.view, Base64.DEFAULT);
                    bitmap = BitmapFactory.decodeByteArray(bitmapByte, 0, bitmapByte.length);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                imgNewWelFare.setImageBitmap(bitmap);
            }
        }
//        if (!TextUtils.isEmpty(bean.getImgPath())) {
//            if (bean.getImgPath().startsWith("http")) {
//                GlideManager.glide(this.imgNewWelFare.getContext(), this.imgNewWelFare, bean.getImgPath(), new CenterCrop(getContext()), new GlideRoundTransform(this.imgNewWelFare.getContext(), 26));
//            } else {
//                GlideManager.glideFile(this.imgNewWelFare.getContext(), this.imgNewWelFare, new File(bean.getImgPath()), new CenterCrop(getContext()), new GlideRoundTransform(this.imgNewWelFare.getContext(), 26));
//            }
//        }
    }

    @Subscribe
    public void onEventMainThread1(MyEvent.NotifyDeviceNameEdit event) {
        MyLog.e("NotifyDeviceNameEdit event");
        if (!this.bean.equals(event.bean)){
            return;
        }
        index_phone_title.setText(event.title);
        UserInfo userInfo = LoginManager.getInstance().getInfo();
        List<RemoteConnectBean> remoteConnectBeans = userInfo.remoteConnectBeans;
        for (int i = 0; i < remoteConnectBeans.size(); i++) {
            RemoteConnectBean remoteConnectBean = remoteConnectBeans.get(i);
            if (remoteConnectBean.equals(bean)) {
                remoteConnectBean.remark = event.title;
                SharepreferenceUtils.saveClass(App.getContext(), SharepreferenConstants.SHARE_NAME_FILE, SharepreferenConstants.USER_INFO_NODE, userInfo);
            }
        }
    }
}